package au.com.uptick.serendipity.server;

import au.com.uptick.serendipity.server.domain.Account;
import au.com.uptick.serendipity.server.domain.dao.AccountDao;
import com.allen_sauer.gwt.log.client.Log;
import org.apache.log4j.xml.DOMConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

// import org.apache.log4j.Level;
// import org.apache.log4j.Logger;

// import org.apache.log4j.Level;
// import org.apache.log4j.Logger;

public class AccountTestCase {

    private long startTimeMillis;

    @Before
    public void setUp() {

        // The log4j configuration file must be in the Project's root directory
        DOMConfigurator.configure("src/main/resources/log4j.xml");

        // When developing applications with Hibernate, you should almost always work
        // with debug enabled for the category org.hibernate.SQL, or, alternatively,
        // the property hibernate.show_sql enabled.
        // Logger.getLogger("org.hibernate.SQL").setLevel(Level.DEBUG);
        // Logger.getLogger("org.hibernate").setLevel(Level.WARN);
        // Logger.getLogger("org.hibernate.cache").setLevel(Level.WARN);

        startTimeMillis = System.currentTimeMillis();
    }

    @Test
    public void testAccountDao() {
        try {
            Log.debug("testAccountDao()");

//            createAccount();
//            updateAccount();
//            deleteAccount();
            readAccounts();
        } catch (Exception e) {
            Log.error("e: " + e);
            e.printStackTrace();
        }
    }

    private void readAccounts() {
        Log.debug("readAccounts()");

        AccountDao accountDao = new AccountDao();
        List<Account> accounts = accountDao.retrieveAccounts();

        for (Account account : accounts) {
            Log.debug(account.toString());
        }
    }

    public void createAccount() {
        Log.debug("createAccount()");

        Account company1 = new Account();
        company1.setAccountName("ABC Corporation Pty Ltd");
        company1.setAccountNumber("ABN: 111 111 111");
        company1.setMainPhone("(01) 1111 1111");
        company1.setCity("Dixon Park");
        company1.setPrimaryContact("Darren Poyner");
        company1.setEmailPrimaryContact("sales@uptick.com.au");

        Account company2 = new Account();
        company2.setAccountName("DEF Corporation Pty Ltd");
        company2.setAccountNumber("ABN: 222 222 222");
        company2.setMainPhone("(02) 2222 2222");
        company2.setCity("Merewether");
        company2.setPrimaryContact("Scott Davies");
        company2.setEmailPrimaryContact("sales@uptick.com.au");

        Account company3 = new Account();
        company3.setAccountName("GHI Corporation Pty Ltd");
        company3.setAccountNumber("ABN: 333 333 333");
        company3.setMainPhone("(03) 3333 3333 ");
        company3.setCity("Merewether");
        company3.setPrimaryContact("Ross Hodge");
        company3.setEmailPrimaryContact("sales@uptick.com.au");

        Account company4 = new Account();
        company4.setAccountName("JKL Corporation Pty Ltd");
        company4.setAccountNumber("ABN: 444 444 444");
        company4.setMainPhone("(04) 4444 4444");
        company4.setCity("Merewether");
        company4.setPrimaryContact("Graham King");
        company4.setEmailPrimaryContact("sales@uptick.com.au");

        Account company5 = new Account();
        company5.setAccountName("MNO Corporation Pty Ltd");
        company5.setAccountNumber("ABN: 555 555 555");
        company5.setMainPhone("(05) 5555 5555");
        company5.setCity("Sydney");
        company5.setPrimaryContact("Sean Doyle");
        company5.setEmailPrimaryContact("sales@uptick.com.au");

        AccountDao accountDao = new AccountDao();
        accountDao.createAccount(company1);
        accountDao.createAccount(company2);
        accountDao.createAccount(company3);
        accountDao.createAccount(company4);
        accountDao.createAccount(company5);

        List<Account> accounts = accountDao.retrieveAccounts();

        for (Account account : accounts) {
            Log.debug(account.toString());
        }
    }

    public void updateAccount() {
        Log.debug("updateAccount()");

        AccountDao accountDao = new AccountDao();

        Account company1 = accountDao.retrieveAccount(1L);
        Log.debug("accountDao.retrieveAccount(1L) - " + company1.toString());
        company1.setAccountNumber("000 000 000");

        Account company2 = accountDao.updateAccount(company1);
        Log.debug("caccountDao.updateAccount(company1) - " + company2.toString());

        List<Account> accounts = accountDao.retrieveAccounts();

        for (Account account : accounts) {
            Log.debug(account.toString());
        }
    }

    public void deleteAccount() {
        Log.debug("deleteAccount()");

        AccountDao accountDao = new AccountDao();

        accountDao.deleteAccount(accountDao.retrieveAccount(1L));

        List<Account> accounts = accountDao.retrieveAccounts();

        for (Account account : accounts) {
            Log.debug(account.toString());
        }
    }

    @After
    public void tearDown() {
        long endTimeMillis = System.currentTimeMillis();
        float durationSeconds = (endTimeMillis - startTimeMillis) / 1000F;
        Log.debug("Duration: " + durationSeconds + " seconds");
    }
}

